package com.questionGenerator.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.questionGenerator.commom.constant.QuestionType;
import com.questionGenerator.dto.req.AddQuestionReqDTO;
import com.questionGenerator.dto.req.QuestionReqDTO;
import com.questionGenerator.dto.resp.QuestionRespDTO;
import com.questionGenerator.mapper.QuestionMapper;
import com.questionGenerator.model.QuestionDO;
import com.questionGenerator.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: usagi
 * @Date: 2024/04/18/23:54
 * @Description:
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, QuestionDO> implements QuestionService {
    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    QuestionType questionType;
    @Override
    public IPage<QuestionRespDTO> queryQuestionByCondition(QuestionReqDTO questionReqDTO) {
        LambdaQueryWrapper<QuestionDO> queryWrapper = Wrappers.lambdaQuery(QuestionDO.class)
                .like(QuestionDO::getKnowledgePoint,questionReqDTO.getKnowledgePoint())
                .eq(QuestionDO::getTypeId,questionReqDTO.getTypeId())
                .eq(QuestionDO::getLanguage,questionReqDTO.getLanguage());
        //Page<QuestionDO> page = new Page<>(questionReqDTO.getCurrent(),questionReqDTO.getSize());
        Page<QuestionDO> questionDOPage = baseMapper.selectPage(questionReqDTO, queryWrapper);
        //questionRespDTOS = BeanUtil.copyToList(questionDOS, QuestionRespDTO.class).var;
        //return questionDOPage;

        return questionDOPage.convert(each -> {
            QuestionRespDTO result = BeanUtil.toBean(each, QuestionRespDTO.class);
            result.setType(questionType.getQuestionTypeName().get(result.getTypeId()-questionType.getMinNum()));
            return result;
        });
    }

    @Override
    public Integer queryCountByKnowledgePoint(String point) {
        point = "%"+point+"%";
        return questionMapper.selectCountByKnowledgePoint(point);
    }

    @Override
    public Integer queryCountByTypeId(Integer typeId) {
        return questionMapper.selectCountByTypeId(typeId);
    }

    @Override
    public Integer queryCountByLanguage(String language) {
        return questionMapper.selectCountByLanguage(language);
    }

    @Override
    public boolean addNewQuestion(AddQuestionReqDTO addQuestionReqDTO) {
        QuestionDO questionDO = QuestionDO.builder()
                .knowledgePoint(addQuestionReqDTO.getKnowledgePoint())
                .typeId(addQuestionReqDTO.getTypeId())
                .content(addQuestionReqDTO.getContent())
                .answer(addQuestionReqDTO.getAnswer())
                .language(addQuestionReqDTO.getLanguage()).build();
        int insert = questionMapper.insert(questionDO);
        return insert>0?true:false;
    }
}
